$(function () {

    // 事间组件
    var laydate = layui.laydate;

    //执行一个laydate实例
    laydate.render({
        elem: '#articleDate' //指定元素
    });

    //上传文件
    $('#inputCover').on('change', function () {
        let files = this.files[0]
        let src = URL.createObjectURL(files)
        $('#coverimg').attr('src', src)
    })


    //渲染分类类别
    $.ajax({
        url: 'http://localhost:8080/api/v1/admin/category/list',
        headers: {  //请求头
            //获取用户数据要带着token过去
            Authorization: localStorage.getItem('token')
        },
        success: function (res) {
            console.log(res);
            let selCategory = template('classify', res)
            $('#category').html(selCategory)
        }
    })


    //富文本渲染
    tinymce.init({
        selector: '#articleContent',  //id带井号与textareaid值一致
        language: 'zh_CN',//注意大小写
    });

    //发布绑定事件
    $('.btn-edit').on('click', function () {
        //前三个参数获取
        let formdata = new FormData($('#form')[0])

        // 后两个拼接
        // 富文本拼接
        formdata.append('content', tinyMCE.editors['articleContent'].getContent())
        //发布拼接
        formdata.append('state', '已发布')

        $.ajax({

            url: 'http://localhost:8080/api/v1/admin/article/publish',
            type: 'post',
            data: formdata,
            headers: {  //请求头
                //获取用户数据要带着token过去
                Authorization: localStorage.getItem('token')
            },
            processData: false,
            contentType: false,
            success: function (res) {
                if (res.code == 200) {
                    location.assign('article_list.html')
                }
            }
        })


    })

})